using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Pavilion
{
    public class ArtificialMouse : MouseCursor
    {
        public const string Name = "ArtificialMouse";

        private ButtonState leftButton, rightButton;
        private bool leftButtonPressPending, rightButtonPressPending;

        public ArtificialMouse() : base()
        {
            leftButtonPressPending = rightButtonPressPending = true;

            position.X = 550f;
            position.Y = 300f;
        }

        public override void Update(GameTime gameTime)
        {
            if (leftButtonPressPending)
            {
                if (ButtonState.Pressed == LeftButton)
                {
                    OnLeftButtonPressed();
                    leftButtonPressPending = false;
                }
            }
            else
            {
                if (ButtonState.Released == LeftButton)
                {
                    OnLeftButtonReleased();
                    leftButtonPressPending = true;
                }
            }

            if (rightButtonPressPending)
            {
                if (ButtonState.Pressed == RightButton)
                {
                    OnLeftButtonPressed();
                    leftButtonPressPending = false;
                }
            }
            else
            {
                if (ButtonState.Released == RightButton)
                {
                    OnRightButtonReleased();
                    rightButtonPressPending = true;
                }
            }
        }

        public override void setPosition(float X, float Y)
        {
            if (position.X != X || position.Y != Y)
            {
                position.X = X;
                position.Y = Y;

                OnMouseMoved();
            }
        }

        public ButtonState LeftButton
        {
            get { return leftButton; }
            set
            {
                leftButton = value;

                if (leftButton == ButtonState.Pressed)
                    OnLeftButtonPressed();
                else
                    OnLeftButtonReleased();
            }
        }

        public ButtonState RightButton
        {
            get { return rightButton; }
            set
            {
                rightButton = value;

                if (rightButton == ButtonState.Pressed)
                    OnRightButtonPressed();
                else
                    OnRightButtonReleased();
            }
        }
    }
}
